package com.junjiao.util.web;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理request返回参数
 * 
 * @author jiaojun
 * @email eshore_jiaojun@163.com
 * @version 2.0 2010-6-13
 */
public class ParamUtil {
	
	/**
	 * @description:  是否为POST方式提交的方法 <br>
	 * @author:jiaojun
	 * @param request
	 * @return
	 */
	public static boolean isPostMethod(HttpServletRequest request){
		return "POST".equals(request.getMethod());
	}
	
	public static String getString(HttpServletRequest request, String paramName) {
		String temp = request.getParameter(paramName);
		if (temp != null && !temp.equals("")) {
			return temp;
		} else {
			return null;
		}
	}

	public static String getString(HttpServletRequest request,
			String paramName, String defaultString) {
		String temp = getString(request, paramName);
		if (temp == null) {
			temp = defaultString;
		}
		return temp;
	}

	public static int getInt(HttpServletRequest request, String paramName)
			throws NumberFormatException {
		return Integer.parseInt(getString(request, paramName));
	}

	public static int getInt(HttpServletRequest request, String paramName,
			int defaultInt) {
		try {
			String temp = getString(request, paramName);
			if (temp == null) {
				return defaultInt;
			} else {
				return Integer.parseInt(temp);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static long getLong(HttpServletRequest request, String paramName)
			throws NumberFormatException {
		return Long.parseLong(getString(request, paramName));
	}

	public static long getLong(HttpServletRequest request, String paramName,
			int defaultInt) {
		try {
			String temp = getString(request, paramName);
			if (temp == null) {
				return defaultInt;
			} else {
				return Long.parseLong(temp);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 防止用户提交带有恶意的脚本，可以先过滤HTML标签，过滤掉双引号，单引号，符号&，符号<，符号
	 */
	public static String htmlEncode(String str) {
		String temp = "";
		if (str == null && !"".equals(str))
			return "";
		temp = str.replace("<", "&lt;").replace(">", "&gt;");
		return temp;
	}

	public static String htmlDecode(String str) {
		String temp = "";
		if (str == null && !"".equals(str))
			return "";
		temp = str.replace("&lt;", "<").replace("&gt;", ">");
		return temp;
	}

	/**
	 * 过滤字符串中非中文，英文，数字的其他字符
	 */
	public static String decideString(String str) {
		String temp = "";
		if (str == null && !"".equals(str))
			return "";
		for (int i = 0; i < str.length(); i++) {
			if (ParamUtil.isChinese(str.charAt(i))
					|| Character.isDigit(str.charAt(i))
					|| Character.isLetter(str.charAt(i)))
				temp = temp + str.charAt(i);
		}
		return temp;
	}

	public static boolean isChinese(char a) {
		int v = (int) a;
		return (v >= 19968 && v <= 171941);
	}

	public static boolean isNumber(char c) {
		return Character.isDigit(c);
	}

	public static boolean isEnglish(char c) {
		return Character.isLetter(c);
	}

	/**
	 * 按字节长度截取字符串
	 * 
	 * @param str
	 *            将要截取的字符串参数
	 * @param toCount
	 *            截取的字节长度
	 * @return 返回截取后的字符串
	 */
	public static String substring(String str, int size) {
		if (str == null || str.length() < 1 || size <= 2) {
			return str;
		}
		int count = 0;// 记录是否截取完
		String returnStr = "";
		str = str.replace("&nbsp;", "");
		for (int i = 0, j = 0; j < size && j < str.getBytes().length;) {
			String ch = str.charAt(i++) + "";

			if (returnStr.getBytes().length == size - 1
					&& ch.getBytes().length > 1) {
				returnStr = returnStr + ".";
			} else {
				returnStr = returnStr + ch;
			}

			j = j + ch.getBytes().length;
			count = j;
		}
		if (str.getBytes().length > count + 1)
			returnStr += "...";

		return ParamUtil.htmlEncode(returnStr);
	}

}
